<?PHP

if($_GET["debug"]){
error_reporting(E_ALL);
ini_set('display_errors', '1');
}


global $config,$root_path, $core,$user,$document, $searchResults;

$config=(object)array();

include_once 'config/defaults.config.php';
include_once 'config/paths.config.php';
include_once 'config/db.config.php';




if (!isset($_SESSION)) {
  session_start();
}
if(array_key_exists("lang", $_SESSION)){
	$config->defaults["lang"]=$_SESSION["lang"];
	}

include_once $config->paths["core"]."pay_core.php";

$document=$core->getDocument("index.html");

      $params=array(
	"estilos"=>array(
	  "/css/global.css",
	  "/css/estilo.css"),
	"scripts"=>array(
	  )
	);

    $app="mainpage";
    $task="defaultTask";
    $app=$core->getApp($app);


    if(is_object($app)&&method_exists($app,$task)){

      $app->$task($params);
      $document->addAlerts();
    }

$app=array_key_exists("app",$_POST)?$_POST["app"]:$_GET["app"];
$task=trim(array_key_exists("task",$_POST)?$_POST["task"]:$_GET["task"],"/");

switch($app){
  case "lang":
	$_SESSION["lang"]=$task;
	  if($_SERVER["HTTP_REFERER"]){
	  	header("location:".$_SERVER["HTTP_REFERER"]);
	  }
	  else {
	  	header("location:/");
	  }
	  exit;
    break;
  case "cache":
    $app=$core->getApp("cache");
    if($app){
      $app->serve($_GET);
    }
    exit;
    break;
  case "search":
    $app=$core->getApp("search");
    if($app){
      $app->search($task);
    }
    break;
  case "recorre":
	  $document=$core->getDocument("recorre.html");
      $document->addScript("http://maps.googleapis.com/maps/api/js?sensor=true&libraries=places&amp;language=es");
	  $searchResults=$core->getApp("search");
	  $searchResults->search($task);
      $document->addStyle("/css/recorre.css");
      $document->addAlerts();
	  break;
  default:
    $handler=$core->getHandler($app);
	  if($handler){
	  	$handler->run($task);
	  }
	  else {
	    $result["error"]="2";
	    $result["errorDescription"]="Aplicacion Desconocida";
	  }
    break;
}


	$document->addStyle("/css/estilo.css");
	$document->addStyle("/css/colorbox.css");
	$document->addScript("/js/jquery-1.8.2.js");
	$document->addScript("/js/jquery.colorbox-min.js");

$document->out();

?>